$(document).ready(function() {
	window.folder = $("#picFolderSelect :selected").val();
	function uploadFiles(folder){
		$('#file_upload').uploadify({
			'uploader'  : 'uploadify/uploadify.swf',
			'script'    : 'uploadPictures.php',
			'cancelImg' : 'uploadify/cancel.png',
			'buttonImg' : 'style/browse.jpg',
			'folder'    : folder,
			'auto'      : false,
			'fileExt'     : '*.jpg;*.gif;*.png;*.bmp;*.tif',
  			'fileDesc'    : 'Image Files,jpg,gif,png,bmp',
			'multi'		:true,
			'removeCompleted':true,
			'onSelectOnce':function(event,data){
				$fileNumber = data.fileCount;
				if($fileNumber>0){
					if($(".uploadbuttonwhole").css("display")=="none"){
						$(".uploadbuttonwhole").css("display","inline");
					}
				}else{
					$(".uploadbuttonwhole").css("display","none");
				}
			},
			'onCancel':function(event,ID,fileObj,data){
				$fileNumber = data.fileCount;
				if($fileNumber>0){
					if($(".uploadbuttonwhole").css("display")=="none"){
						$(".uploadbuttonwhole").css("display","inline");
					}
				}else{
					$(".uploadbuttonwhole").css("display","none");
				}
			},
			'onAllComplete':function(event,data){
				$("#endPicUpload").html("<img src='style/success-msg.png' /> 共上传"+data.filesUploaded+"张图片，"+HHL.floatNumberFilter(data.allBytesLoaded/1024,2)+"K,平均速度"+ HHL.floatNumberFilter(data.speed,2)+"Kb/s");
				completeUploadChange(folderId);
			}
		});
	}
	uploadFiles(folder);
	function completeUploadChange(folderId){
		window.folderId=folderId;
		var url = "pictureManageAjax.php";
		var data = {"folderId":folderId,"postType":3};
		$.ajax({
			type:"POST",
			url:url,
			data:data,
			success:function(data){
				$("#picturefolderInfoSpan_"+folderId).html(data);
			}
		});
	}
	var folderName = $("#picFolderSelect :selected").html();
	$("#uploadTargetInfoDiv").html("上传到<span class='uploadPicInfoFont'>"+folderName+"</span>相册");
	function getTargetFolder(){
		var folder = $("#picFolderSelect :selected").val();
		$('#file_upload').uploadifySettings('folder',folder);
		
		$('#file_upload').uploadifyClearQueue();
		var folderName = $("#picFolderSelect :selected").html();
		$("#uploadTargetInfoDiv").html("上传到<span class='uploadPicInfoFont'>"+folderName+"</span>相册");
		$("#endPicUpload").html("");
	}
	$("#picFolderSelect").change(getTargetFolder);
	
	
	$("#pictureControlBUpload").click(displayUploadDiv);
	function displayUploadDiv(){
		if($("#picUpload").css("display")=="none"){
			$("body").append("<div id='wholemark'></div>");
			var position = HHL.getPosition();
			$("#wholemark").css({"width":position.Tw,"height":position.Th});
			var left = position.Tw/4;
			$("#picUpload").css({"position":"absolute","display":"block","left":left+"px","top":"60px"});
		}else{
			$("#picUpload").css({"display":"none"});
			$("#wholemark").remove();
		}
	}
	window.displayUploadDiv=displayUploadDiv;
	
	$("#closeUploadDiv").click(closeUploadDiv);
	function closeUploadDiv(){
		$("#picUpload").css({"display":"none"});
		$('#file_upload').uploadifyClearQueue();
		$("#wholemark").remove();
	}
	$("#uploadPics").click(function(){
		$('#file_upload').uploadifyUpload();
	}
	);
	
	
	//display pictures in the target folder;
	var folderArr = $(".picturefolder");
	//HHL.newMes(folderArr.length)
	window.folderHandle=0;
	function disPlayPicS(folderId){
		var data={"folderId":folderId,"postType":2};
		var url= "pictureManageAjax.php";
		window.folderId= folderId;
		$.ajax({
			type:"POST",
			data:data,
			url:url,
			success:function(data){
				folderHandle++;
				$("body").append("<div id='folderPicsDisplay_"+folderId+"' class='folderPicsDisplay'>"+data+"</div>");
				var left = 150+folderHandle*5;
				var top = 80+folderHandle*5;
				$("#folderPicsDisplay_"+folderId).css({"left":left,"top":"0","margin-bottom":"100px"});
			}
		})
	}
	window.disPlayPicS=disPlayPicS;
	
	function closPicsDisplayFolder(handle){
		var folderDiv = "folderPicsDisplay_"+handle;
		$("#"+folderDiv).remove();
	}
	window.closPicsDisplayFolder=closPicsDisplayFolder
	
	function dynamicUploadFiles(folder,inputId,divId){
		
		$('#'+inputId).uploadify({
			'uploader'  : 'uploadify/uploadify.swf',
			'script'    : 'uploadPictures.php',
			'cancelImg' : 'uploadify/cancel.png',
			'folder'    : folder,
			'auto'      : false,
			'multi'		:true,
			'fileExt'     : '*.jpg;*.gif;*.png;*.bmp;*.tif',
  			'fileDesc'    : 'Image Files,jpg,gif,png,bmp',
			'removeCompleted':true,
			'onSelectOnce':function(event,data){
				$fileNumber = data.fileCount;
				if($fileNumber>0){
					if($(".uploadButton").css("display")=="none"){
						$(".uploadButton").css("display","inline");
					}
				}else{
					$(".uploadButton").css("display","none");
				}
			},
			'onCancel':function(event,ID,fileObj,data){
				$fileNumber = data.fileCount;
				if($fileNumber>0){
					if($(".uploadButton").css("display")=="none"){
						$(".uploadButton").css("display","inline");
					}
				}else{
					$(".uploadButton").css("display","none");
				}
			},
			'onAllComplete':function(event,data){
				$("#"+divId).append("<img src='style/success-msg.png' /> 共上传"+data.filesUploaded+"张图片，"+HHL.floatNumberFilter(data.allBytesLoaded/1024,2)+"K,平均速度"+HHL.floatNumberFilter(data.speed,2)+"Kb/s");
				completeUploadChangeMore(folderId);
			}
		});
	}
	function completeUploadChangeMore(folderId){
		window.folderId=folderId;
		var url = "pictureManageAjax.php";
		var data = {"folderId":folderId,"postType":4};
		$.ajax({
			type:"POST",
			url:url,
			data:data,
			success:function(data){
				var needle = data.indexOf(";")
				var picNumber = data.substring(0,needle);
				$("#picturefolderInfoSpan_"+folderId).html(picNumber);
				needle+=1;
				if($("#picsList_"+folderId).html()){
					$("#picsList_"+folderId).html(data.substr(needle));
				}else{
					$("#folderPicsDisplay_"+folderId).append("<div id='picsList_"+folderId+"'>"+data.substr(needle)+"</div>");
				}
			}
		});
	}
	
	function createDynamicUpload(folderId,inputId,divId){
		
		dynamicUploadFiles(folderId,inputId,divId);
	}
	window.createDynamicUpload=createDynamicUpload;
	
	//display single target picture
	function displayTargetPic(picId,folderId){
		window.picId = picId;
		window.folderId = folderId;
		var url = "pictureManageAjax.php";
		var data = {"picId":picId,"folderId":folderId,"postType":5};
		$.ajax({
			type:"POST",
			url:url,
			data:data,
			beforeSend:function(){
				HHL.newMes("<img src='config/images/loading.gif' alt='waiting...' />...","z-index:2000");
			},
			success:function(data){
				$("#kindMessage000").remove();
				
				if($("#displaySingleTargPic").html()){
					$("#targetPicDis").css({"visibility":"hidden"});
					$("#targetPicDis").html(data);
					
					var width = $(window).width();
					var height = $(window).height();
					var targetPicWidth = $("#targetPicDisDo").width();
					var targetPicHeight = $("#targetPicDisDo").height();
					width-=200;
					if(targetPicWidth>=width){
						var less =targetPicWidth-width+200;
						var oldtargetPicWidth =targetPicWidth;
						targetPicWidth=targetPicWidth-less;
						var lessBei = oldtargetPicWidth/targetPicWidth;
						var targetPicHeight = $("#targetPicDisDo").height();
						targetPicHeight =targetPicHeight/lessBei;
						$("#targetPicDisDo").width(targetPicWidth);
						$("#targetPicDisDo").height(targetPicHeight);
						 
					} 
					$("#targetPicDis").css({"display":"none"});
					$("#targetPicDis").css({"visibility":"visible"});
					
					$("#targetPicDis").fadeIn("1800");
					 
					var width = $(window).width();
					var targetPicWidth = $("#targetPicDisDo").width();
					var targetPicHeight = $("#targetPicDisDo").height();
					var marginL = (width-targetPicWidth)/2;
					var marginT = (height-targetPicHeight)/2;
					marginL=marginL>0?marginL:0;
					marginT = marginT>0?marginT:0;
					$("#targetPicDis").css({"margin-left":marginL,"margin-top":marginT});
					$("#targetPicDisDo").focus();
				}else{
					$("body").append("<div id='wholemark'></div>");
					var postion = HHL.getPosition();
					$("#wholemark").css({"width":postion.Tw,"height":postion.Th,"background":"grey","z-index":11});
					var width = $(window).width();
					var height = $(window).height();
					$("body").append("<div id='displaySingleTargPic'><div id='targetPicDis'>"+data+"</div></div>");
					
					$("#targetPicDis").css({"visibility":"hidden"});
					
					$("#displaySingleTargPic").css({"width":width,"height":height});
					var targetPicWidth = $("#targetPicDisDo").width();
					var targetPicHeight = $("#targetPicDisDo").height();
					var targetPicHeight = $("#targetPicDisDo").height();
					width-=200;
					if(targetPicWidth>=width){
						var less =targetPicWidth-width+200;
						var oldtargetPicWidth =targetPicWidth;
						targetPicWidth=targetPicWidth-less;
						var lessBei = oldtargetPicWidth/targetPicWidth;
						var targetPicHeight = $("#targetPicDisDo").height();
						targetPicHeight =targetPicHeight/lessBei;
						$("#targetPicDisDo").width(targetPicWidth);
						$("#targetPicDisDo").height(targetPicHeight);
					}
					
					$("#targetPicDis").css({"display":"none"});
					$("#targetPicDis").css({"visibility":"visible"});
					$("#targetPicDis").fadeIn("1800");
					var width = $(window).width();
					var targetPicHeight = $("#targetPicDisDo").height();
					var targetPicHeight = $("#targetPicDisDo").height();
					var marginL = (width-targetPicWidth)/2;
					var marginT = (height-targetPicHeight)/2;
					marginL=marginL>0?marginL:0;
					marginT = marginT>0?marginT:0;
					$("#targetPicDis").css({"margin-left":marginL,"margin-top":marginT});
					var scrollL = $(document).scrollLeft();
					var scrollT = $(document).scrollTop();
					$("#displaySingleTargPic").css({"left":scrollL,"top":scrollT});
					$("#targetPicDisDo").focus();
				}
			}
		});
	}
	window.displayTargetPic=displayTargetPic;
	
	 function closePicSingleDisplay(divId){
	 	$("#"+divId).remove();
	 	$("#wholemark").remove();
	 }
	 window.closePicSingleDisplay=closePicSingleDisplay;
	
	 //delete target picture with id
	 function deleteTargetPic(picId,folderId){
	 	var deleteconfirm = confirm("确定删除？");
	 	if(!deleteconfirm){
	 		return false;
	 	}
	 	var url = "pictureManageAjax.php";
	 	var data={"picId":picId,"postType":6};
	 	window.picId = picId;
	 	window.folderId =folderId;
	 	$.ajax({
	 		type:"POST",
	 		url:url,
	 		data:data,
	 		success:function(data){
	 			if(data=="success"){
	 				$("div[idhandle="+picId+"]").remove();
	 				var num = parseInt($("#picturefolderInfoSpan_"+folderId).html())-1;
	 				$("#picturefolderInfoSpan_"+folderId).html(num);
	 				HHL.newAlertMes("删除成功","z-index:20;","1500");
	 			}else{
	 				HHL.newAlertMes("删除失败","z-index:20;","1500");
	 			}
	 		}
	 	})
	 }
	 window.deleteTargetPic=deleteTargetPic;
	 
	 function setFolderCover(picId,folderId){
	 	var url = "pictureManageAjax.php";
	 	var data={"picId":picId,"folderId":folderId,"postType":7};
	 	window.picId = picId;
	 	window.folderId =folderId;
	 	$.ajax({
	 		type:"POST",
	 		url:url,
	 		data:data,
	 		success:function(data){
	 			if(data){
	 				 HHL.newAlertMes("设置成功","z-index:20;","1500");
	 				 $("#folder_"+folderId+" img").attr("src",data);
	 			}else{
	 				HHL.newAlertMes("系统繁忙，请稍后再试","z-index:20;","1500");
	 			}
	 		}
	 	})
	 }
	 window.setFolderCover=setFolderCover;
	 
	 function deletePicFolder(folderId){
	 	//delete picture folder;
	 	var confirmDelete = confirm("确定删除？");
	 	if(!confirmDelete){
	 		return false;
	 	}
	 	var url = "pictureManageAjax.php";
	 	var data={"folderId":folderId,"postType":8};
	 	window.folderId =folderId;
	 	$.ajax({
	 		type:"POST",
	 		url:url,
	 		data:data,
	 		success:function(data){
	 			if(data){
	 				 HHL.newAlertMes("删除成功","z-index:20;","1500");
	 				 $("#folder_"+folderId).remove();
	 				 $("#folderPicsDisplay_"+folderId).remove();
	 			}else{
	 				HHL.newAlertMes("系统繁忙，请稍后再试","z-index:20;","1500");
	 			}
	 		}
	 	})
	 }
	 window.deletePicFolder=deletePicFolder;
});